import Vue from 'vue'
import Router from 'vue-router'
// import Login from './components/login.vue'
const Login = () => import(/* webpackChunkName: "Login" */ './components/login.vue')
// import Home from './components/Home.vue'
// import Welcome from './components/Welcome.vue'
const Home = () => import(/* webpackChunkName: "Home-Welcome" */ './components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Home-Welcome" */ './components/Welcome.vue')
// import Users from './components/user/Users.vue'
// import Rights from './components/rights/Rights.vue'
// import Roles from './components/rights/Roles.vue'
const Users = () => import(/* webpackChunkName: "Users-Rights-Roles" */ './components/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users-Rights-Roles" */ './components/rights/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users-Rights-Roles" */ './components/rights/Roles.vue')
// import Cate from './components/goods/Cate.vue'
// import Params from './components/goods/Params.vue'
// import Goods from './components/goods/Goods.vue'
const Cate = () => import(/* webpackChunkName: "Cate-Params-Goods" */ './components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "Cate-Params-Goods" */ './components/goods/Params.vue')
const Goods = () => import(/* webpackChunkName: "Cate-Params-Goods" */ './components/goods/Goods.vue')
// import Add from './components/goods/Add.vue'
// import Orders from './components/Orders/Orders.vue'
// import Reports from './components/Reports.vue'
const Add = () => import(/* webpackChunkName: "Add-Orders-Reports" */ './components/goods/Add.vue')
const Orders = () => import(/* webpackChunkName: "Add-Orders-Reports" */ './components/Orders/Orders.vue')
const Reports = () => import(/* webpackChunkName: "Add-Orders-Reports" */ './components/Reports.vue')

Vue.use(Router)

const router = new Router({
  // mode: 'history', // 去掉url上的#
  routes: [{
    path: '/',
    redirect: '/login'
  }, {
    path: '/login',
    component: Login
  }, {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: Goods },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports }

    ]
  }]
})
// 路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to)// 访问的地址
  var token = sessionStorage.getItem('token')
  if (to.path === '/login') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
